package com.nhjf.mobile.interceptor;
import java.util.Enumeration;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sf.ehcache.constructs.blocking.LockTimeoutException;
import net.sf.ehcache.constructs.web.AlreadyCommittedException;
import net.sf.ehcache.constructs.web.AlreadyGzippedException;
import net.sf.ehcache.constructs.web.filter.FilterNonReentrantException;
import net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter;
/**
 * 页面缓存过滤器
 * @version 1.0
 */
public class PageEhCacheFilter extends SimplePageCachingFilter {
    private static final Logger LOG = LoggerFactory.getLogger(PageEhCacheFilter.class);
     
    @Override
    protected void doFilter(final HttpServletRequest request, final HttpServletResponse response,
            final FilterChain chain) throws AlreadyGzippedException, AlreadyCommittedException,
            FilterNonReentrantException, LockTimeoutException, Exception {
        if (isCacheAble(request.getRequestURI())) {
            LOG.debug("缓存url:" + request.getRequestURI());
            super.doFilter(request, response, chain);
        } else {
            chain.doFilter(request, response);
        }
    }
    @Override
    protected String calculateKey(HttpServletRequest httpRequest) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(httpRequest.getMethod()).append(httpRequest.getRequestURI()).append(httpRequest.getQueryString());
        String key = stringBuffer.toString();
        return key;
    }
    private boolean isCacheAble(String requestURI) {
           //通过正则表达式判断是否缓存该页面
//		if (requestURI.indexOf("/courses/coursespage") >= 0) {
//			return true;
//		}else if(requestURI.indexOf("/courses/coursesDetails") >= 0){
//			return true;
//		}
    	
//        String[] cacheUrl = new String[] { 
//                "^/$", "^/\\d+/$",
//                "^/[A-Za-z_]+/$","^/[A-Za-z_]+/\\d+/$",//包含java和seo路径
//                "^/about/$"};
//        for (String string : cacheUrl) {
//            if (requestURI.matches(string)) {
//                return true;
//            }
//        }
        return false;
    }
    /**
     * Checks if request contains the header value.
     */
    private boolean headerContains(final HttpServletRequest request, final String header, final String value) {
        logRequestHeaders(request);
        final Enumeration accepted = request.getHeaders(header);
        while (accepted.hasMoreElements()) {
            final String headerValue = (String) accepted.nextElement();
            if (headerValue.indexOf(value) != -1) {
                return true;
            }
        }
        return false;
    }
    /**
     * @see net.sf.ehcache.constructs.web.filter.Filter#acceptsGzipEncoding(javax.servlet.http.HttpServletRequest)
     * 兼容ie6/7 gzip压缩
     * @author hoojo
     * @createDate 2012-7-4 上午11:07:11
     */
    @Override
    protected boolean acceptsGzipEncoding(HttpServletRequest request) {
        boolean ie6 = headerContains(request, "User-Agent", "MSIE 6.0");
        boolean ie7 = headerContains(request, "User-Agent", "MSIE 7.0");
        return acceptsEncoding(request, "gzip") || ie6 || ie7;
    }
}